package pers.cj.pdos.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import pers.cj.pdos.controller.base.BaseController;
import pers.cj.pdos.controller.entity.ResponseData;
import pers.cj.pdos.controller.entity.ResponseEntity;
import pers.cj.pdos.controller.entity.ResponseList;
import pers.cj.pdos.entity.Company;
import pers.cj.pdos.entity.CustomType;
import pers.cj.pdos.entity.Unit;
import pers.cj.pdos.service.ICustomService;
import pers.cj.pdos.service.ICustomTypeService;
import pers.cj.pdos.utils.DataUtil;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author chenj
 * @since 2019-11-04
 */
@Controller
@RequestMapping("/custom")
public class CustomTypeController extends BaseController {
    @Autowired
    ICustomTypeService customTypeService;

    @RequestMapping("/customType")
    @RequiresPermissions("customType:query")
    public String customType() {
        return "/custom/customType";
    }

    @ResponseBody
    @RequiresPermissions("customType:query")
    @RequestMapping("/customType/getList")
    public ResponseList getList(int page, int limit) {
        Company company = getCompany();

        List<CustomType> list = customTypeService.listPage(page, limit, company.getId());

        //返回数据
        return ResponseList.success(customTypeService.count(company.getId()), list);
    }

    @ResponseBody
    @RequiresPermissions("customType:insert")
    @RequestMapping("/customType/add")
    public ResponseEntity add(@RequestBody CustomType customType) {
        Company company = getCompany();
        if (customTypeService.checkName(customType.getName(),company.getId())){
            return new ResponseEntity(2,"名称已存在");
        }
        DataUtil.processEntity(company,customType);
        customTypeService.save(customType);
        return ResponseEntity.success();
    }

    @ResponseBody
    @RequiresPermissions("customType:close")
    @RequestMapping("/customType/delete")
    public ResponseEntity delete(@RequestBody CustomType customType) {
        try {
            DataUtil.processEntity(false,getCompany(),customType);
            customTypeService.removeById(customType.getId());
            return ResponseEntity.success();
        } catch (Exception e) {
            return ResponseEntity.fail("已被使用无法删除");
        }
    }

    @ResponseBody
    @RequiresPermissions("customType:insert")
    @RequestMapping("/customType/update")
    public ResponseData update(@RequestBody CustomType customType) {
        Company company = getCompany();
        CustomType old = customTypeService.getById(customType.getId());
        if (!old.getName().equals(customType.getName()) && customTypeService.checkName(customType.getName(),company.getId())){
            return ResponseData.fail("单位名称已存在");
        }

        DataUtil.processEntity(false,company,customType);
        customTypeService.updateById(customType);
        return ResponseData.success(customType);
    }

    @ResponseBody
    @RequestMapping("/customType/getAll")
    public ResponseData getAll() {
        Company company = getCompany();
        //返回数据
        return ResponseData.success(customTypeService.list(new QueryWrapper<CustomType>().eq("company_id",company.getId())));
    }

}
